package gin_api

import (
	"fmt"
	"gitee.com/fenglei-l/ops-util/xxlog"
	"github.com/gin-gonic/gin"
	"net/http"
)

type Response struct {
	Code   int         `json:"code"`
	Result interface{} `json:"result"`
	Msg    string      `json:"msg"`
}
type ResponsePage struct {
	Code   int         `json:"code"`
	Result interface{} `json:"result"`
	Total  int64       `json:"total"`
	Msg    string      `json:"msg"`
}

const (
	ERROR       = 1
	SUCCESS     = 0
	ERROR_LOGIN = -9
)

func result(c *gin.Context, code int, data interface{}, msg string) {
	c.JSON(http.StatusOK, Response{
		code,
		data,
		msg,
	})
	c.Abort()
	return
}
func resultPage(c *gin.Context, code int, data interface{}, total int64, msg string) {
	c.JSON(http.StatusOK, ResponsePage{
		code,
		data,
		total,
		msg,
	})
	c.Abort()
	return
}
func OkAnyData(c *gin.Context, contentType string, data string) {
	c.Data(http.StatusOK, contentType, []byte(data))
	return
}
func OkJson(c *gin.Context, content interface{}) {
	c.JSON(http.StatusOK, content)
	return
}
func StatusJson(c *gin.Context, status int, content ...interface{}) {
	c.JSON(status, content)
	return
}
func FailWithData(c *gin.Context, msg string) {
	xxlog.ErrorUp366(fmt.Errorf("request:%s %s", c.Request.RequestURI, msg))
	result(c, ERROR, map[string]interface{}{}, msg)
}
func OkWithData(c *gin.Context, content interface{}) {
	result(c, SUCCESS, content, "success")
}
func NoLoginData(c *gin.Context, msg string) {
	c.JSON(http.StatusUnauthorized, Response{
		ERROR_LOGIN,
		map[string]interface{}{},
		msg,
	})
	c.Abort()
}
func OkWithDataPage(c *gin.Context, content interface{}, total int64) {
	resultPage(c, SUCCESS, content, total, "success")
}
